//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Transactions;
using System.Threading;
using System.Security;
using System.Globalization;

using Shp.Runtime.Contract;

namespace Shp.Runtime.Services
{
    public class DatamodelService : IDatamodelUC, IDatamodelSC
    {
        public StringPair[][] GetInstanceList(string tenantAlias, string entityDefName, Guid[] listToRetrieve)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            if (string.IsNullOrEmpty(tenantAlias))
                throw new ArgumentNullException("tenantAlias");
            if (string.IsNullOrEmpty(entityDefName))
                throw new ArgumentNullException("entityDefName");
            if (listToRetrieve.Length < 1)
                throw new ArgumentOutOfRangeException("listToRetrieve");

            return DatamodelLogic.GetInstanceList(tenantAlias, entityDefName, listToRetrieve);
        }

        public StringPair[] GetViewDefFields(string tenantAlias, string viewDefName)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            if (string.IsNullOrEmpty(tenantAlias))
                throw new ArgumentNullException("tenantAlias");
            if (string.IsNullOrEmpty(viewDefName))
                throw new ArgumentNullException("viewDefName");

            return DatamodelLogic.GetViewDefFields(tenantAlias, viewDefName);
        }

        public StringPair[] GetViewDefFieldsById(string tenantAlias, Guid viewDefId)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            if (string.IsNullOrEmpty(tenantAlias))
                throw new ArgumentNullException("tenantAlias");
            if (Guid.Empty == viewDefId)
                throw new ArgumentNullException("viewDefId");

            return DatamodelLogic.GetViewDefFields(tenantAlias, viewDefId);
        }

        public EntityField[] GetEntityDefFields(string tenantAlias, string entityDefName)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            if (string.IsNullOrEmpty(tenantAlias))
                throw new ArgumentNullException("tenantAlias");
            if (string.IsNullOrEmpty(entityDefName))
                throw new ArgumentNullException("entityDefName");

            return DatamodelLogic.GetEntityDefFields(tenantAlias, entityDefName);
        }

        public Dictionary<Guid, string> GetEntityDefList()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            return DatamodelLogic.GetEntityDefList();
        }

        public Dictionary<Guid, string> GetViewDefList()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            return DatamodelLogic.GetViewDefList();
        }

        public GuidStringPair[] GetTypeList()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            return DatamodelLogic.GetTypeList();
        }

        public void UpdateViewDef(Guid viewDefId, StringPair[] fields)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();
            if (viewDefId == Guid.Empty)
                throw new ArgumentNullException("viewDefId");
            if (fields.Length < 1)
                throw new ArgumentNullException("fields");

            DatamodelLogic.UpdateViewDef(tenantId, viewDefId, fields);
        }

        public void UpdateEntityDef(Guid entityDefId, EntityField[] fields)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();
            if (entityDefId == Guid.Empty)
                throw new ArgumentNullException("entityDefId");
            if (fields.Length < 1)
                throw new ArgumentNullException("fields");

            DatamodelLogic.UpdateEntityDef(tenantId, entityDefId, fields);
        }

        public Guid AddInstance(string entityDefName, EntityField[] fields)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw (new SecurityException());
            if (String.IsNullOrEmpty(entityDefName))
                throw (new ArgumentNullException("entityDefName"));
            if (fields.Length < 1)
                throw (new ArgumentOutOfRangeException("fields"));

            using (TransactionScope scope = new TransactionScope())
            {
                Guid instanceId = DatamodelLogic.AddInstance(tenantId, entityDefName, fields);
                scope.Complete();
                return instanceId;
            }
        }

    }
}